#pragma once
#include "stdafx.h"
#include "one.h"



#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


IplImage *imagem = 0, *hsvm = 0, *huem = 0, *maskm = 0, *backprojectm = 0, *histimgm = 0;
CvHistogram *hist = 0;


int select_object = 0;

int show_hist = 1;
CvPoint origin;

CvRect track_window;
CvBox2D track_box;
CvConnectedComp track_comp;
int hdims = 16;
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;





CvScalar hsv2rgb( float hue )
{
    int rgb[3], p, sector;
    static const int sector_data[][3]=
        {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
    hue *= 0.033333333333333333333333333333333f;
    sector = cvFloor(hue);
    p = cvRound(255*(hue - sector));
    p ^= sector & 1 ? 255 : 0;

    rgb[sector_data[sector][0]] = 255;
    rgb[sector_data[sector][1]] = 0;
    rgb[sector_data[sector][2]] = p;

    return cvScalar(rgb[2], rgb[1], rgb[0],0);
}

void mean_( IplImage* frame,CvRect selection ,int track_object,int backproject_mode,int vmin,int vmax,int smin)
{
   

       



        
        int i, bin_w;

       
        if( !frame )
            return;

        if( !imagem)
        {
            /* allocate all the buffers */
            imagem = cvCreateImage( cvGetSize(frame), 8, 3 );
            imagem->origin = frame->origin;
            hsvm = cvCreateImage( cvGetSize(frame), 8, 3 );
            huem = cvCreateImage( cvGetSize(frame), 8, 1 );
            maskm = cvCreateImage( cvGetSize(frame), 8, 1 );
            backprojectm = cvCreateImage( cvGetSize(frame), 8, 1 );
            hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
            histimgm = cvCreateImage( cvSize(320,200), 8, 3 );
            cvZero( histimgm );
        }

        cvCopy( frame, imagem, 0 );
        cvCvtColor( imagem, hsvm, CV_BGR2HSV );

        if( track_object )
        {
            int _vmin = vmin, _vmax = vmax;

            cvInRangeS( hsvm, cvScalar(0,smin,MIN(_vmin,_vmax),0),
                        cvScalar(180,256,MAX(_vmin,_vmax),0), maskm );
            cvSplit( hsvm, huem, 0, 0, 0 );

            if( track_object < 0 )
            {
                float max_val = 0.f;
                cvSetImageROI( huem, selection );
                cvSetImageROI( maskm, selection );
                cvCalcHist( &huem, hist, 0, maskm );
                cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );
                cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 );
                cvResetImageROI( huem );
                cvResetImageROI( maskm );
                track_window = selection;
                track_object = 1;

                cvZero( histimgm );
                bin_w = histimgm->width / hdims;
                for( i = 0; i < hdims; i++ )
                {
                    int val = cvRound( cvGetReal1D(hist->bins,i)*histimgm->height/255 );
                    CvScalar color = hsv2rgb(i*180.f/hdims);
                    cvRectangle( histimgm, cvPoint(i*bin_w,histimgm->height),
                                 cvPoint((i+1)*bin_w,histimgm->height - val),
                                 color, -1, 8, 0 );
                }
            }

            cvCalcBackProject( &huem, backprojectm, hist );
            cvAnd( backprojectm, maskm, backprojectm, 0 );
            cvCamShift( backprojectm, track_window,
                        cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
                        &track_comp, &track_box );
            track_window = track_comp.rect;

            if( backproject_mode )
                cvCvtColor( backprojectm, imagem, CV_GRAY2BGR );
            if( !imagem->origin )
                track_box.angle = -track_box.angle;
           
			cvCircle(imagem, cvPoint(track_box.center.x,track_box.center.y),4,cvScalar(50,255,50,5),2);
           // cvEllipseBox( imagem, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 );
        }

        if( select_object && selection.width > 0 && selection.height > 0 )
        {
            cvSetImageROI( imagem, selection );
            cvXorS( imagem, cvScalarAll(255), imagem, 0 );
            cvResetImageROI( imagem );
        }


        if(backproject_mode==0)
		{
        cvCopy(imagem,frame,0);
		}

		if(backproject_mode==1)
		{
        cvCvtPlaneToPix(backprojectm, backprojectm, backprojectm, 0, frame );
		}


    
}

